/*
 * $Id: LocaleForm.java 109 2009-06-30 15:05:54Z donbeave $
 */

package labour.manager.client.widget.form;

import labour.concept.client.data.Locale;

import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FormEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextField;

public class LocaleForm extends FormPanel {

  private LabelField idField;
  private TextField<String> langField;
  private TextField<String> abbrField;
  private TextField<String> charsetField;

  private Locale model;

  public LocaleForm() {
    setHeading("Locale");

    setFrame(true);

    idField = new LabelField();
    idField.setFieldLabel("ID");
    add(idField);

    langField = new TextField<String>();
    langField.setFieldLabel("Language");
    langField.setAllowBlank(false);
    add(langField);

    abbrField = new TextField<String>();
    abbrField.setFieldLabel("Abbreviation");
    abbrField.setAllowBlank(false);
    add(abbrField);

    charsetField = new TextField<String>();
    charsetField.setFieldLabel("Charset");
    charsetField.setAllowBlank(false);
    add(charsetField);

    addListener(Events.BeforeSubmit, new Listener<FormEvent>() {
      public void handleEvent(FormEvent formEvent) {
        if (LocaleForm.this.isValid()) {
          getItem().getModel().setLanguage(getLanguageField().getValue());
          getItem().getModel().setAbbreviation(getAbbreviationField().getValue());
          getItem().getModel().setCharset(getCharsetField().getValue());
        }
      }
    });
  }

  public LocaleForm(Locale model) {
    this();

    setItem(model);
  }

  public LabelField getIdField() {
    return idField;
  }

  public TextField<String> getLanguageField() {
    return langField;
  }

  public TextField<String> getAbbreviationField() {
    return abbrField;
  }

  public TextField<String> getCharsetField() {
    return charsetField;
  }

  public Locale getItem() {
    return model;
  }

  public void setItem(Locale item) {
    this.model = item;

    getIdField().setValue(item.getModel().getId());
    getLanguageField().setValue(item.getModel().getLanguage());
    getAbbreviationField().setValue(item.getModel().getAbbreviation());
    getCharsetField().setValue(item.getModel().getCharset());
  }

}
